<?php

declare (strict_types=1);

namespace App\Model;


class OrderReview extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'order_review';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $guarded = [];


    public static $name = [
        'en' => 'anonymous',
        'zh' => '匿名用户'
    ];

    public static function getFavorableList($uid)
    {

        $select = [
            'a.comment',
            'a.rating',
            'a.anonymous',
            'a.create_time',
            'b.head',
            'b.nickname'
        ];

        $where [] = ['a.rating', '>=', 4];
        $where [] = ['a.aid', '=', $uid];

        $list = self::from('order_review as a')
            ->leftJoin('user_datum as b', 'a.uid', '=', 'b.uid')
            ->where($where)
            ->select($select)
            ->orderByRaw('CHAR_LENGTH(zx_a.comment) desc')
            ->limit(3)
            ->get()
            ->toArray();


        foreach ($list as $k => &$v) {

            if ($v['anonymous'] == 1) {
                $v['head'] = defaultHead();
                $v['nickname'] = self::$name[getLang()];
            } else {
                $v['nickname'] = $str = mb_substr($v['nickname'], 0, 1, 'UTF-8') . '***';
            }
        }


        return $list;
    }


    /**
     * 获取评价列表
     * @param $uid
     * @return array
     */
    public static function getEvaluateList($uid)
    {

        $select = [
            'a.comment',
            'a.rating',
            'a.anonymous',
            'a.create_time',
            'b.head',
            'b.nickname'
        ];

        $where [] = ['a.aid', '=', $uid];

        $model = self::from('order_review as a')
            ->leftJoin('user_datum as b', 'a.uid', '=', 'b.uid')
            ->where($where)
            ->select($select)
            ->orderByRaw('zx_a.rating desc');

        $list = page($model);
        if (!empty($list['list'])){

            foreach ($list['list'] as $k=>$v){

                if ($v['anonymous'] == 1) {
                    $list['list'][$k]['head'] = defaultHead();
                    $list['list'][$k]['nickname'] = self::$name[getLang()];
                } else {
                    $list['list'][$k]['nickname'] = $str = mb_substr($v['nickname'], 0, 1, 'UTF-8') . '***';
                }
            }

        }
        return $list;

    }


}
